package com.brillilab.starter.controller.aio.kit;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.inventory.InventoryRecordEnum;
import com.brillilab.domain.pa.kit.KitSpecAddPa;
import com.brillilab.domain.vo.kit.SampleInfoVo;
import com.brillilab.domain.vo.kit.SampleSpecVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryRecordService;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.logic.kit.GlobalKitLogic;
import com.brillilab.service.logic.kit.KitSearchLogic;
import com.brillilab.service.logic.kit.SampleLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;

/**
 * kit搜索
 * 
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/kit/search")
public class KitSearchController {
	@Autowired
	private UsersLogic usersLogic;
	@Autowired
	private KitSearchLogic kitSearchLogic;
	@Autowired
	private IKitService kitService;
	@Autowired
	private GlobalKitLogic globalKitLogic;
	@Autowired
	private SampleLogic sampleLogic;
	@Autowired
	private IInventoryRecordService inventoryRecordService;
	/**
	 * 搜索试剂列表
	 *
	 * @param itemNo
	 *            货号
	 * @return
	 */
	@RequestMapping(value = "/ragent/list", method = RequestMethod.GET)
	public ResponseVo searchReagentList(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam String itemNo) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		return ResponseVo.success(kitSearchLogic.searchReagentList(urs.getLabId(), itemNo));
	}

	
	/**
	 * 新增试剂规格
	 * 
	 * @param pa
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/reagent/addSpec", method = RequestMethod.POST)
	public ResponseVo addReagentSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestBody KitSpecAddPa pa) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		pa.setLabId(urs.getLabId());
		pa.setCreatorId(urs.getLabMemberId());
		if (pa.getIsLabExist() == 1) {
			kitService.assertIsLabKit(urs.getLabId(), pa.getKitId());
		}
		Long kitId = kitSearchLogic.addReagentSpec(pa);
		if(pa.getIsLabExist() == 1) {
			inventoryRecordService.add(urs, kitId, InventoryRecordEnum.ADD_SPEC);
		}else {
			inventoryRecordService.add(urs, kitId, InventoryRecordEnum.CREATE);
		}
		return ResponseVo.successMap("kitId", kitId);
	}

	/**
	 * 获取试剂规格信息列表
	 * 
	 * @param kitId
	 *            kitId
	 * @param isLabExist
	 *            是否实验室已有：0-否，1-是
	 * @return
	 */
	@RequestMapping(value = "/reagent/spec", method = RequestMethod.GET)
	public ResponseVo getReagentSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Integer isLabExist, @RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		if (isLabExist == 1) {
			kitService.assertIsLabKit(urs.getLabId(), kitId);
		}
		return ResponseVo.success(kitSearchLogic.getReagentSpec(isLabExist, kitId));
	}

	/**
	 * 全局试剂信息详情
	 * 
	 * @param kitId
	 * @return
	 */
	@RequestMapping(value = "/reagent/details", method = RequestMethod.GET)
	public ResponseVo getReagentInfo(@RequestParam Long kitId) {
		return ResponseVo.success(globalKitLogic.getReagentDetails(kitId));
	}

	/**
	 * 试剂导入
	 * 
	 * @param labId
	 *            实验室ID
	 * @param globalKitId
	 *            globalKitId
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/reagent/import", method = RequestMethod.GET)
	public ResponseVo reagentImport(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitId = kitSearchLogic.reagentImport(urs.getLabId(), kitId, urs.getLabMemberId());
		inventoryRecordService.add(urs, kitId, InventoryRecordEnum.CREATE);
		return ResponseVo.successMap("kitId",kitId);
	}

	/**
	 * 搜索样品列表
	 * 
	 * @param itemNo
	 *            货号
	 * @return
	 */
	@RequestMapping(value = "/sample/list", method = RequestMethod.GET)
	public ResponseVo searchSampleList(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam String itemNo) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		List<SampleInfoVo> searchList = kitSearchLogic.searchSampleList(urs.getLabId(), itemNo);
		return ResponseVo.success(searchList);
	}

	/**
	 * 样品导入
	 * 
	 * @param labId
	 *            实验室ID
	 * @param kitId
	 *            kitId
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/sample/import", method = RequestMethod.GET)
	public ResponseVo sampleImport(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		SampleSpecVo specVo = kitSearchLogic.sampleImport(urs.getLabId(), kitId, urs.getLabMemberId());
		inventoryRecordService.add(urs, specVo.getKitId(), InventoryRecordEnum.CREATE);
		return ResponseVo.success(specVo);
	}


	/**
	 * 全局样品信息详情
	 * 
	 * @param kitId
	 * @return
	 */
	@RequestMapping(value = "/sample/details", method = RequestMethod.GET)
	public ResponseVo getSampleInfo(@RequestParam Long kitId) {
		return ResponseVo.success(globalKitLogic.getSampleDetails(kitId));
	}
	
	/**
	 * 获取样品规格信息
	 * 
	 * @param labId
	 *            实验室ID
	 * @param kitId
	 *            kitId
	 * @return
	 */
	@RequestMapping(value = "/sample/spec", method = RequestMethod.GET)
	public ResponseVo getSampleSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long kitId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		kitService.assertIsLabKit(urs.getLabId(), kitId);
		return ResponseVo.success(sampleLogic.getSampleSpec(kitId));
	}
	
}
